﻿Imports System.Globalization.CultureInfo

Public Class fZadaniPrijmyPolozkyDialog
    'counter of no. of clicks on btn "Další"
    Dim pocetKliknutiBtnDalsi As Long = 0
    'counteer of no. of rows in tables
    Dim pocetRadkuVtab As Long = 0
    'id values of tables' rows
    Dim id_odberatele As Long = 0
    Dim id_poznamky As Long = 0
    Dim id_cisloFaktury As Long = 0
    Dim id_postovne As Long = 0
    Dim id_typPlatby As Long = 0
    Dim id_zpusobProdeje As Long = 0
    Dim id_vyrobek(-1) As Long

    Private Sub fZadaniPrijmyPolozkyDialog_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        My.Forms.fZadaniPrijmy.Dispose()
        My.Forms.fZadaniPrijmy.Close()
        Me.Dispose()
    End Sub

    Private Sub fZadaniPrijmyPolozkyDialog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'loads dtb content
        Me.Seznam_odberateleTableAdapter1.Fill(Me.DtbDataSet1.seznam_odberatele)
        Me.Seznam_poznamkyTableAdapter1.Fill(Me.DtbDataSet1.seznam_poznamky)
        Me.Seznam_PrijemFakturyTableAdapter.Fill(Me.DtbDataSet1.seznam_PrijemFaktury)
        Me.Seznam_PrijemPostovneTableAdapter1.Fill(Me.DtbDataSet1.seznam_PrijemPostovne)
        Me.Seznam_PrijemTypPlatbyTableAdapter1.Fill(Me.DtbDataSet1.seznam_PrijemTypPlatby)
        Me.Seznam_PrijemZpusobProdejeTableAdapter1.Fill(Me.DtbDataSet1.seznam_PrijemZpusobProdeje)
        Me.Seznam_vyrobkyTableAdapter1.Fill(Me.DtbDataSet1.seznam_vyrobky)
        Me.Tab_prijemTableAdapter1.Fill(Me.DtbDataSet1.tab_prijem)
        'sets dims of public array
        ReDim _zadaniPrijmyPolozky(My.Forms.fZadaniPrijmy.chboxFaktVyrobky.CheckedItems.Count() - 1, 2)
        'disables btn "Předchozí" by default
        Me.btnPredchozi.Enabled = False
        'disables btn "Hotovo" by default, except if only one product is selected
        If My.Forms.fZadaniPrijmy.chboxFaktVyrobky.CheckedItems.Count() = 1 Then
            Me.btnHotovo.Enabled = True
            Me.btnDalsi.Enabled = False
        Else
            Me.btnHotovo.Enabled = False
        End If
        'sets max. value of "numboxVyrobekMnozstvi"
        Me.numboxVyrobekMnozstvi.Maximum = 9999
        'loads 1st checked product from chbox
        Me.lblVyrobekJmenoShow.Text = My.Forms.fZadaniPrijmy.chboxFaktVyrobky.CheckedItems.Item(0)

    End Sub

    Private Sub btnDalsi_Click(sender As System.Object, e As System.EventArgs) Handles btnDalsi.Click
        If Me.tboxVyrobekCenaKus.Text.Length > 0 And Me.numboxVyrobekMnozstvi.Value > 0 Then
            Try 'return previous entry, if btn "Předchozí" was clicked before
                If _zadaniPrijmyPolozky(Me.pocetKliknutiBtnDalsi + 1, 0) <> "" Then
                    Me.pocetKliknutiBtnDalsi += 1
                    Me.btnPredchozi.Enabled = True
                    Me.lblVyrobekJmenoShow.Text = _zadaniPrijmyPolozky(Me.pocetKliknutiBtnDalsi, 0)
                    Me.numboxVyrobekMnozstvi.Value = _zadaniPrijmyPolozky(Me.pocetKliknutiBtnDalsi, 1)
                    Me.tboxVyrobekCenaKus.Text = Convert.ToString(_zadaniPrijmyPolozky(Me.pocetKliknutiBtnDalsi, 2))
                    Me.tboxVyrCelkem.Text = Convert.ToString( _
                        Me.numboxVyrobekMnozstvi.Value * Convert.ToDecimal(Me.tboxVyrobekCenaKus.Text))
                    'when clicked "další", saves data into array and prepares form for next filling
                ElseIf Me.pocetKliknutiBtnDalsi < _zadaniPrijmyPolozky.GetLength(0) - 1 Then
                    Fill_zadaniPrijmyPolozky( _
                        Me.lblVyrobekJmenoShow.Text, _
                        Me.numboxVyrobekMnozstvi.Value, _
                        Convert.ToDecimal(Me.tboxVyrobekCenaKus.Text), _
                        Me.pocetKliknutiBtnDalsi)
                    Me.btnPredchozi.Enabled = True
                    Me.pocetKliknutiBtnDalsi += 1
                    Me.lblVyrobekJmenoShow.Text = My.Forms.fZadaniPrijmy.chboxFaktVyrobky.CheckedItems.Item( _
                        Me.pocetKliknutiBtnDalsi)
                    Me.numboxVyrobekMnozstvi.Value = 0
                    Me.tboxVyrobekCenaKus.Text = ""
                    Me.tboxVyrCelkem.Text = ""
                End If
                If Me.pocetKliknutiBtnDalsi = _zadaniPrijmyPolozky.GetLength(0) - 1 Then
                    Me.btnDalsi.Enabled = False
                    Me.btnHotovo.Enabled = True
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba zadání další položky!")
                Exit Sub
                Me.Close()
            End Try
        Else
            MsgBox("Je třeba vyplnit všechny položky!", MsgBoxStyle.Exclamation, "Chyba!")
        End If
        '-------------------------------------------------------------------------

    End Sub


    Private Sub btnPredchozi_Click(sender As System.Object, e As System.EventArgs) Handles btnPredchozi.Click
        Try 'when btn "předchozí" clicked, loads previous data from array
            If Me.pocetKliknutiBtnDalsi > 0 Then
                Me.pocetKliknutiBtnDalsi -= 1
                Me.lblVyrobekJmenoShow.Text = _zadaniPrijmyPolozky(Me.pocetKliknutiBtnDalsi, 0)
                Me.numboxVyrobekMnozstvi.Value = _zadaniPrijmyPolozky(Me.pocetKliknutiBtnDalsi, 1)
                Me.tboxVyrobekCenaKus.Text = Convert.ToString(_zadaniPrijmyPolozky(Me.pocetKliknutiBtnDalsi, 2))
                Me.tboxVyrCelkem.Text = Convert.ToString( _
                    Me.numboxVyrobekMnozstvi.Value * Convert.ToDecimal(Me.tboxVyrobekCenaKus.Text))
                Me.btnDalsi.Enabled = True
                Me.btnHotovo.Enabled = False
                If Me.pocetKliknutiBtnDalsi = 0 Then
                    Me.btnPredchozi.Enabled = False
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba při načítání předchozího záznamu!")
            Me.Close()
        End Try
    End Sub

    Private Sub btnHotovo_Click(sender As System.Object, e As System.EventArgs) Handles btnHotovo.Click
        If Me.tboxVyrobekCenaKus.Text.Length > 0 And Me.numboxVyrobekMnozstvi.Value > 0 Then
            Try 'saves data of last product into array
                Fill_zadaniPrijmyPolozky(Me.lblVyrobekJmenoShow.Text, _
                                         Me.numboxVyrobekMnozstvi.Value, _
                                         Convert.ToDecimal(Trim(Me.tboxVyrobekCenaKus.Text)), _
                                         Me.pocetKliknutiBtnDalsi)
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba zadání poslední položky!")
                My.Forms.fMain.lblTStrip1.Text = NEULOZENO
                GoTo HotovoEnd
            End Try
            '-------------------------------------------------------------------------
            'processing of tables in dtb
            '---------------------------
            
            '---
            'table "seznam_odberatele"
            Try
                Dim tabOdb As DataTable = Me.DtbDataSet1.seznam_odberatele
                Dim jm As String = My.Forms.fZadaniPrijmy.tboxJmeno.Text.Trim
                Dim prij As String = My.Forms.fZadaniPrijmy.tboxPrijmeni.Text.Trim
                Dim adr As TextBox = My.Forms.fZadaniPrijmy.tboxAdresa
                Dim adrText As String = ""
                pocetRadkuVtab = tabOdb.Rows.Count()

                If adr.Lines.Length = 1 Then
                    adrText = adr.Text.Trim
                ElseIf adr.Lines.Length > 1 Then
                    For i As Long = 0 To adr.Lines.GetUpperBound(0)
                        adrText = adrText & "," & adr.Lines(i).Trim
                    Next i
                Else
                    adrText = "N/A"
                End If

                If pocetRadkuVtab > 0 Then
                    For i As Long = 0 To pocetRadkuVtab - 1
                        If tabOdb.Rows.Item(i).Item("jmeno") = jm _
                            And tabOdb.Rows.Item(i).Item("prijmeni") = prij _
                            And tabOdb.Rows.Item(i).Item("adresa") = adrText Then
                            Me.id_odberatele = tabOdb.Rows.Item(i).Item(0)
                            Exit For
                        ElseIf i = pocetRadkuVtab - 1 Then
                            Dim newOdbRow As DataRow = tabOdb.NewRow()
                            Dim cVal As Long = 0
                            'get the highest ID value (in case that ID are garbled)
                            For j As Long = 0 To pocetRadkuVtab - 1
                                Dim id As Long = tabOdb.Rows.Item(j).Item(0)
                                If id > cVal Then
                                    cVal = id
                                End If
                            Next j

                            newOdbRow(0) = cVal + 1
                            newOdbRow(1) = jm
                            newOdbRow(2) = prij
                            newOdbRow(3) = adrText
                            tabOdb.Rows.Add(newOdbRow)
                            Me.Seznam_odberateleTableAdapter1.Update(tabOdb)
                            Me.id_odberatele = tabOdb.Rows.Item(tabOdb.Rows.Count() - 1).Item(0)
                        End If
                    Next i
                Else
                    Dim newOdbRow As DataRow = tabOdb.NewRow()

                    newOdbRow(0) = 1
                    newOdbRow(1) = jm
                    newOdbRow(2) = prij
                    newOdbRow(3) = adrText
                    tabOdb.Rows.Add(newOdbRow)
                Me.Seznam_odberateleTableAdapter1.Update(tabOdb)
                Me.id_odberatele = tabOdb.Rows.Item(tabOdb.Rows.Count() - 1).Item(0)
                End If

            Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání odběratele")
            GoTo HotovoEnd
        End Try
            '---
            'table "seznam_vyrobky"
            Try
                Dim tabVyr As DataTable = Me.DtbDataSet1.seznam_vyrobky
                Dim chboxVyr As CheckedListBox = My.Forms.fZadaniPrijmy.chboxFaktVyrobky
                ReDim Me.id_vyrobek(chboxVyr.CheckedItems.Count() - 1)
                pocetRadkuVtab = tabVyr.Rows.Count()

                For i As Long = 0 To id_vyrobek.GetUpperBound(0)
                    For j As Long = 0 To pocetRadkuVtab - 1
                        If chboxVyr.CheckedItems.Item(i) = _
                            tabVyr.Rows.Item(j).Item(1) Then
                            Me.id_vyrobek(i) = tabVyr.Rows.Item(j).Item(0)
                            Exit For
                        End If
                    Next j
                Next i

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba načítání ID výrobků")
                GoTo HotovoEnd
            End Try
            '---
            'table "seznam_TypPlatby" and "seznam_TypProdeje"
            Me.id_typPlatby = My.Forms.fZadaniPrijmy.cboxZpusobPlatby.SelectedValue
            Me.id_zpusobProdeje = My.Forms.fZadaniPrijmy.cboxTypProdeje.SelectedValue
            '---
            'table "seznam_poznamky"
            Try
                Dim tabPozn As DataTable = Me.DtbDataSet1.seznam_poznamky
                Dim pozn As TextBox = My.Forms.fZadaniPrijmy.tboxPoznamka
                Dim poznText As String = ""
                pocetRadkuVtab = tabPozn.Rows.Count()

                If pozn.Lines.Length = 1 Then
                    poznText = pozn.Text.Trim
                ElseIf pozn.Lines.Length > 1 Then
                    For i As Long = 0 To pozn.Lines.GetUpperBound(0)
                        poznText = poznText & pozn.Lines(i).Trim & ","
                    Next i
                Else
                    poznText = "N/A"
                End If

                If pocetRadkuVtab > 0 Then
                    For i As Long = 0 To pocetRadkuVtab - 1
                        If tabPozn.Rows.Item(i).Item("text_poznamky") = _
                            poznText Then
                            Me.id_poznamky = tabPozn.Rows.Item(i).Item(0)
                            Exit For
                        ElseIf i = pocetRadkuVtab - 1 Then
                            Dim newPoznRow As DataRow = tabPozn.NewRow()
                            Dim cVal As Long = 0

                            For j As Long = 0 To pocetRadkuVtab - 1
                                Dim id As Long = tabPozn.Rows.Item(j).Item(0)
                                If id > cVal Then
                                    cVal = id
                                End If
                            Next j

                            newPoznRow(0) = cVal + 1
                            newPoznRow(1) = poznText
                            tabPozn.Rows.Add(newPoznRow)
                            Me.Seznam_poznamkyTableAdapter1.Update(tabPozn)
                            Me.id_poznamky = tabPozn.Rows.Item(tabPozn.Rows.Count() - 1).Item(0)
                        End If
                    Next i
                Else
                    Dim newPoznRow As DataRow = tabPozn.NewRow()
                    newPoznRow(0) = 1
                    newPoznRow(1) = poznText
                    tabPozn.Rows.Add(newPoznRow)
                    Me.Seznam_poznamkyTableAdapter1.Update(tabPozn)
                    Me.id_poznamky = tabPozn.Rows.Item(tabPozn.Rows.Count() - 1).Item(0)
                End If

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání poznámky")
                GoTo HotovoEnd
            End Try
            '---
            'table "seznam_PrijemPostovne"
            Try
                Dim tabPost As DataTable = Me.DtbDataSet1.seznam_PrijemPostovne
                Dim post As TextBox = My.Forms.fZadaniPrijmy.tboxPostovne
                Dim postVal As Decimal = Convert.ToDecimal(post.Text.Trim)
                pocetRadkuVtab = tabPost.Rows.Count()

                If pocetRadkuVtab > 0 Then
                    For i As Long = 0 To pocetRadkuVtab - 1
                        If tabPost.Rows.Item(i).Item("postovne_hodnota") = _
                            postVal Then
                            Me.id_postovne = tabPost.Rows.Item(i).Item(0)
                            Exit For
                        ElseIf i = pocetRadkuVtab - 1 Then
                            Dim newPostRow As DataRow = tabPost.NewRow()
                            Dim cVal As Long = 0

                            For j As Long = 0 To pocetRadkuVtab - 1
                                Dim id As Long = tabPost.Rows.Item(j).Item(0)
                                If id > cVal Then
                                    cVal = id
                                End If
                            Next j

                            newPostRow(0) = cVal + 1
                            newPostRow(1) = postVal
                            tabPost.Rows.Add(newPostRow)
                            Me.Seznam_PrijemPostovneTableAdapter1.Update(tabPost)
                            id_postovne = tabPost.Rows.Item(tabPost.Rows.Count() - 1).Item(0)
                        End If
                    Next i
                Else
                    Dim newPostRow As DataRow = tabPost.NewRow()
                    newPostRow(0) = 1
                    newPostRow(1) = postVal
                    tabPost.Rows.Add(newPostRow)
                    Me.Seznam_PrijemPostovneTableAdapter1.Update(tabPost)
                    id_postovne = tabPost.Rows.Item(tabPost.Rows.Count() - 1).Item(0)
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání poštovného")
                GoTo HotovoEnd
            End Try
            '---
            'table "seznam_PrijemFaktury"
            Try
                Dim tabFakt As DataTable = Me.DtbDataSet1.seznam_PrijemFaktury
                Dim cislFakt As String = My.Forms.fZadaniPrijmy.tboxCisloFaktury.Text.Trim
                Dim newRow As DataRow = tabFakt.NewRow()
                pocetRadkuVtab = tabFakt.Rows.Count()
                Dim cVal2 As Long = 0
                For i As Long = 0 To pocetRadkuVtab - 1
                    Dim num As Long = tabFakt.Rows.Item(i).Item(0)
                    If num > cVal2 Then
                        cVal2 = num
                    End If
                Next
                newRow(0) = cVal2 + 1
                newRow(1) = cislFakt

                Me.id_cisloFaktury = newRow(0)

                tabFakt.Rows.Add(newRow)
                Me.Seznam_PrijemFakturyTableAdapter.Update(tabFakt)


            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání čísla faktury")
                GoTo HotovoEnd
            End Try
            'table "tab_prijem"

            Try
                Dim tabPrijem As DataTable = Me.DtbDataSet1.tab_prijem

                For i As Long = 0 To id_vyrobek.GetUpperBound(0)
                    pocetRadkuVtab = tabPrijem.Rows.Count()
                    Dim newRowPrijem As DataRow = tabPrijem.NewRow()
                    If pocetRadkuVtab = 0 Then
                        newRowPrijem(0) = 1
                    Else
                        'get highest ID value and increment it
                        'needs to be used, since data are SELECTed ASC by datum_prodeje, not by id
                        Dim cVal As Long = 1
                        For j As Long = 0 To pocetRadkuVtab - 1
                            Dim id As Long = tabPrijem.Rows.Item(j).Item(0)
                            If id > cVal Then
                                cVal = id
                            End If
                        Next j
                        newRowPrijem(0) = cVal + 1
                    End If
                    newRowPrijem(1) = id_cisloFaktury
                    newRowPrijem(2) = id_odberatele
                    newRowPrijem(3) = id_vyrobek(i)
                    newRowPrijem(4) = id_typPlatby
                    newRowPrijem(5) = id_zpusobProdeje
                    newRowPrijem(6) = id_poznamky
                    newRowPrijem(7) = id_postovne
                    newRowPrijem(8) = My.Forms.fZadaniPrijmy.dpickDatumProdeje.Value.Date
                    newRowPrijem(9) = My.Forms.fZadaniPrijmy.dpickDatumSplatnosti.Value.Date
                    newRowPrijem(10) = _zadaniPrijmyPolozky(i, 1)
                    newRowPrijem(11) = _zadaniPrijmyPolozky(i, 2)
                    tabPrijem.Rows.Add(newRowPrijem)
                    Me.Tab_prijemTableAdapter1.Update(tabPrijem)
                Next i
                My.Forms.fMain.lblTStrip1.Text = ULOZENO

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba ukládání do tabulky příjmů")
                'rollback of tab "seznam_PrijemFaktury" is needed
                Dim rowDel As DataRow = Me.DtbDataSet1.seznam_PrijemFaktury.Rows.Find(id_cisloFaktury)
                Me.DtbDataSet1.seznam_PrijemFaktury.Rows.Remove(rowDel)
                Me.Seznam_PrijemFakturyTableAdapter.Update(Me.DtbDataSet1.seznam_PrijemFaktury)
                GoTo HotovoEnd
            End Try

            '----------------end
            '---------------------------------------
            '------closes this and parent form
HotovoEnd:  My.Forms.fZadaniPrijmy.Dispose()
            My.Forms.fZadaniPrijmy.Close()
            Me.Close()
        Else
            MsgBox("Je třeba vyplnit všechny položky!", MsgBoxStyle.Exclamation, "Chyba!")
        End If
    End Sub

    Private Sub tboxVyrobekCenaKus_TextChanged(sender As Object, e As System.EventArgs) Handles tboxVyrobekCenaKus.TextChanged
        Dim cena As TextBox = tboxVyrobekCenaKus
        If cena.Text.Length > 0 Then
            If Decimal.TryParse(cena.Text.Trim, Decimal.One) = False Then
                If cena.Text.Contains(",") = True Then
                    cena.Text = cena.Text.Replace(",", ".")
                    cena.SelectionStart = cena.Text.Length
                    cena.Select()

                    tboxVyrCelkem.Text = (numboxVyrobekMnozstvi.Value * _
                        Convert.ToDecimal(cena.Text.Trim)).ToString

                    If Decimal.TryParse(cena.Text.Trim, Decimal.One) = False Then
                        MsgBox("Desetinné číslo nezadáno ve správném formátu", MsgBoxStyle.Critical, "Chyba")
                        Exit Sub
                    End If
                End If
            Else
                tboxVyrCelkem.Text = (numboxVyrobekMnozstvi.Value * _
                        Convert.ToDecimal(cena.Text.Trim)).ToString
            End If


        End If
    End Sub
End Class